//#include <setparam.h>
#include <osa_cmem.h>
#include <audio_dac_priv.h>
#include <drv_i2c.h>

int AUDIODAC_init(DRV_I2cHndl *i2cHndl)
{
  int k;
  Uint8 regAddr[16];
  Uint8 regVal[16];  

  //Program the audio DAC device on I2C

  k=0;
  regAddr[k]  = 0x01;
  regVal[k++] = 0x80; //Reset

  regAddr[k]  = 0x02;
  regVal[k++] = 0x00;  //ADC/DAC Sample Rate = Fsref/1

  regAddr[k]  = 0x03;
  regVal[k++] = 0x10;  //PLL Q Value = 2
  
  regAddr[k]  = 0x07;
  regVal[k++] = 0x0A;  //Fsref=48kHz, 
                       //Left/Right DAC datapath plays left/right 
					   //channel input data (unmuted)

  regAddr[k]  = 0x08;
  regVal[k++] = 0x00; //BCLK and WCLK are inputs (slave mode)

  regAddr[k]  = 0x09;
  regVal[k++] = 0x08; //I2S mode, 16 bits, 256 Bits/Frame (in master mode only)

  regAddr[k]  = 0x0A;
  regVal[k++] = 0x00; //Word Offset Control in TDM mode (for slots 1 & 2)

  regAddr[k]  = 0x25;
  regVal[k++] = 0xC0; //Power up Left and Right DACs

  regAddr[k]  = 0x29;
  regVal[k++] = 0xA0; //Left/Right DAC output selects DAC_L2/DAC_R2 
                      //path to left/right high power output drivers

  regAddr[k]  = 0x2B;
  regVal[k++] = 0x00; //Left DAC unmuted, digital Volume Control = 0dB

  regAddr[k]  = 0x2C;
  regVal[k++] = 0x00; //Right DAC unmuted, digital Volume Control = 0dB

  regAddr[k]  = 0x33;
  regVal[k++] = 0x0D; //HPLOUT output level control = 0dB

  regAddr[k]  = 0x41;
  regVal[k++] = 0x0D; //HPROUT output level control = 0dB

  regAddr[k]  = 0x65;
  regVal[k++] = 0x01; //CODEC_CLKIN uses CLKDIV_OUT

  regAddr[k]  = 0x66;
  regVal[k++] = 0xA2; //CLKDIV_IN uses BCLK, PLLCLK _IN uses BCLK (instead of MCLK)    
  
  return DRV_audioDacWrite(i2cHndl, regAddr, regVal, k);
}

